package org.westboy.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Modality;
import org.springframework.beans.factory.annotation.Autowired;
import org.westboy.App;
import org.westboy.common.base.BaseController;
import org.westboy.common.base.MediaService;
import org.westboy.common.base.OrderContext;
import org.westboy.common.task.OrderSoundService;
import org.westboy.common.util.DateUtil;
import org.westboy.model.Order;
import org.westboy.service.OrderService;
import org.westboy.view.DetailView;
import org.westboy.view.NoteView;

import java.util.Date;
import java.util.Random;


@FXMLController
public class IndexController extends BaseController{

    @FXML
    private TableView tableView;

    @FXML
    private TableColumn iconColumn;

    @FXML
    private TableColumn idColumn;

    @FXML
    private TableColumn orderIdColumn;

    @FXML
    private TableColumn statusColumn;

    @FXML
    private TableColumn priceColumn;

//    @FXML
//    private TableColumn buyerColumn;

    @FXML
    private TableColumn consigneeColumn;

    @FXML
    private TableColumn createTimeColumn;

    @FXML
    private TableColumn desColumn;

    @FXML
    private TableColumn orderNoteColumn;

    @FXML
    private Button refreshBtn;

    @FXML
    private Button closeBth;

    @FXML
    private Label todayOrderCountLabel;

    @FXML
    private Label todayLabel;

    @FXML
    private Label statusLabel;

    @FXML
    private Label countLabel;

    @FXML
    private Label word;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderSoundService soundService;


    @Autowired
    private MediaService mediaService;

    @Autowired
    private OrderContext orderContext;

    @FXML
    @Override
    protected void initialize() {

     initTableView();
     initOther();
     soundService.start();
     countLabel.textProperty().bind(soundService.messageProperty());
     closeBth.visibleProperty().bind(soundService.isPlayProperty());
     statusLabel.textProperty().bind(soundService.titleProperty());
    }

    private void initOther(){
//     refreshBtn.setGraphic(new ImageView(new Image(getClass().getResource("/icons/refresh_16x16.png").toExternalForm())));
     word.setText(ws[new Random().nextInt(7)]);
     todayOrderCountLabel.setText(orderService.findTodayOrderCount().toString());
     todayLabel.setText("今天是"+ DateUtil.format(new Date(),"yyyy年MM月dd日"));

    }

    private void initTableView(){
     tableView.setItems(orderService.findTodayOrder());
     iconColumn.setCellValueFactory(new PropertyValueFactory<>("icon"));
     idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
     orderIdColumn.setCellValueFactory(new PropertyValueFactory<>("orderId"));
     statusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
     priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
//     buyerColumn.setCellValueFactory(new PropertyValueFactory<>("buyer"));
     consigneeColumn.setCellValueFactory(new PropertyValueFactory<>("consignee"));
     createTimeColumn.setCellValueFactory(new PropertyValueFactory<>("createTime"));
     desColumn.setCellValueFactory(new PropertyValueFactory<>("des"));
     orderNoteColumn.setCellValueFactory(new PropertyValueFactory<>("orderNote"));
    }

    @FXML
    private void doRefreshAction(){
     initOther();
     tableView.setItems(orderService.findTodayOrder());
     tableView.refresh();
    }

    @FXML
    private void doStopSound(){
        mediaService.play();
        OrderSoundService.resetLastSum();
    }


    @FXML
    private void showItemMent(MouseEvent event){
        if(event.getButton().equals(MouseButton.SECONDARY)){
            ContextMenu contextMenu = new ContextMenu();
            MenuItem detailMenu = new MenuItem("订单详情");

            detailMenu.setOnAction((ActionEvent e) -> {
                Order bean = (Order) tableView.getSelectionModel().getSelectedItem();
                System.out.println("选择订单:"+bean.getOrderId());
                orderContext.getOrder().setOrderId(bean.getOrderId());
                orderContext.getOrder().setPrice(bean.getPrice());
                orderContext.getOrder().setStatus(bean.getStatus());
                orderContext.getOrder().setDes(bean.getDes());
                orderContext.getOrder().setCreateTime(bean.getCreateTime());
                orderContext.getOrder().setConsignee(bean.getConsignee());
                orderContext.getOrder().setBuyer(bean.getBuyer());
                orderContext.getOrder().setOrderNote(bean.getOrderNote());
                orderContext.setList(orderService.findOrderProList(bean.getOrderId()));
                App.showView(DetailView.class, Modality.WINDOW_MODAL);
            });
            MenuItem noteMenu = new MenuItem("订单备注");
            noteMenu.setOnAction((ActionEvent e) -> {
                Order bean = (Order) tableView.getSelectionModel().getSelectedItem();
                System.out.println("选择订单:"+bean.getOrderId());
                orderContext.getOrder().setId(bean.getId());
                orderContext.getOrder().setOrderId(bean.getOrderId());
                orderContext.getOrder().setPrice(bean.getPrice());
                orderContext.getOrder().setStatus(bean.getStatus());
                orderContext.getOrder().setDes(bean.getDes());
                orderContext.getOrder().setCreateTime(bean.getCreateTime());
                orderContext.getOrder().setConsignee(bean.getConsignee());
                orderContext.getOrder().setBuyer(bean.getBuyer());
                orderContext.getOrder().setOrderNote(bean.getOrderNote());
                orderContext.setList(orderService.findOrderProList(bean.getOrderId()));
                App.showView(NoteView.class, Modality.WINDOW_MODAL);
            });
            contextMenu.getItems().addAll(detailMenu,noteMenu);
            tableView.setContextMenu(contextMenu);
        }
    }
}
